<template>
  <div class='m-lesson_classes'>
    <div style='padding-bottom: 20px;'>
      <el-button type='primary' @click='add'>添加班级</el-button>
    </div>
    <a-table :data='data' :cols='tableKeys'>
      <el-table-column align="center" label="操作" width='180' slot='ahead'>
        <template slot-scope="scope">
          <div>
            <el-button size='mini' @click='choose(scope.row.id)'>选择</el-button>
            <el-button size='mini' @click='deleteObj(scope.row.id)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'

export default {
  name: 'm-lesson_classes',

  data() {
    return {
      tableKeys: [
        { prop: 'name', label: '班级名称', minWidth: '100' },
        { prop: 'teachers', label: '老师', minWidth: '150' },
        { prop: 'memo', label: '备注', minWidth: '100' }
      ],
      data: [],
      ifRequesting: false
    }
  },

  mounted() {
    this.$refs.page.changePage()
  },

  methods: {
    ...mapMutations(['setTabRoutes']),

    routerPush(type, args) {
      // push 到缓存里
      this.setTabRoutes({ type: 'add', obj: { ...this.getRouterInfoAccordingName(type), ...args } })
    },

    async fetchData(aPageNum, aPageSize) {
      return this.$api.courseGetClasses({
        aMerchantId: this.aFirmId,
        aPageNum,
        aPageSize
      })
    },

    getData(data) {
      this.data = data
    },

    add() {
      this.routerPush('lesson_classesAdd')
      this.$router.push({ name: 'lesson_classesAdd' })
    },

    choose(id) {
      const query = { type: 'set', id }
      this.routerPush('lesson_classesDetail', { query })
      this.$router.push({ name: 'lesson_classesDetail', query })
    },

    deleteObj(id) {
      if (this.ifRequesting) return false
      this.__messageBox({
        title: '你确认要删除该班级吗？',
        successCallback: async () => {
          this.ifRequesting = true
          const { code } = await this.$api.courseDeleteRooms({ aId: id })
          this.ifRequesting = false
          this.$refs.page.changePage()
          return code
        }
      })
    }
  }
}
</script>

<style lang='less'>

</style>
